package com.yisu.expressway.onedollar.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.pizidea.imagepicker.bean.ImageItem;
import com.yisu.expressway.R;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class OrderShowPicGridListAdapter extends BaseAdapter
        implements View.OnClickListener, View.OnLongClickListener {

    public static final String CAMER_URL = "camer_url";
    private int countLimit = 10;
    private Context mContext;
    private List<ImageItem> mDataList = new ArrayList<>();
    private PicItemClickListener mPicItemClickListener;

    public OrderShowPicGridListAdapter(Context context, List<ImageItem> data,
                                       PicItemClickListener picItemClickListener) {
        mContext = context;
        mDataList = data;
        mPicItemClickListener = picItemClickListener;
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public ImageItem getItem(int position) {
        return mDataList == null ? null : mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setCountLimit(int countLimit) {
        this.countLimit = countLimit;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        holder = new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.grid_item_pic, null);
        holder.imgView = (ImageView) convertView.findViewById(R.id.iv_pic);
        convertView.setTag(holder);
        holder.imgView.setTag(R.id.iv_pic, position);
        holder.imgView.setOnClickListener(this);
        holder.imgView.setOnLongClickListener(this);

        holder.imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageItem imageItem = getItem(position);
        if (!TextUtils.isEmpty(imageItem.path)) {
            if (CAMER_URL.equals(imageItem.path)) {
                holder.imgView.setImageDrawable(
                        mContext.getResources().getDrawable(R.drawable.pic_add));
                if (getCount() >= countLimit) {
                    holder.imgView.setVisibility(View.GONE);
                }
            } else {
                Glide.with(mContext).load(imageItem.path).asBitmap().placeholder(R.drawable.blank)
                        .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
                        .animate(R.anim.fade_in_image).into(holder.imgView);
            }

        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_pic:
                int position = (Integer) v.getTag(v.getId());
                if (mPicItemClickListener != null) {
                    String path = mDataList.get(position).path;
                    if (CAMER_URL.equals(path)) {
                        mPicItemClickListener.onCameraItemClick();
                    } else {
                        mPicItemClickListener.onPicItemClick(path,position);
                    }

                }
                break;
            default:
                break;
        }
    }

    public void setData(List<ImageItem> data) {
        this.mDataList = data;
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.iv_pic:
                int position = (Integer) v.getTag(v.getId());
                if (mPicItemClickListener != null) {
                    String path = mDataList.get(position).path;
                    if (!CAMER_URL.equals(path)) {
                        mPicItemClickListener.onPicItemLongClick(path,position);
                    }

                }
                break;
            default:
                break;
        }
        return false;
    }

    public interface PicItemClickListener {
        void onPicItemClick(String picUrl, int position);

        void onPicItemLongClick(String picUrl, int position);

        void onCameraItemClick();
    }

    public class ViewHolder {
        public ImageView imgView;
    }
}
